C++ while(in>>word)
>; 是定向输入, >>;前面是源,后面是目标in>>word就是将in的内容读取到word内和<<;一样就像cout,cin都属于io流,即iostream而上例的in是定义的ifstream,属于fstream即文件流所以in>>word相当于cin>>word只不过输入的方式不一样in被定义成文件流,cin是标准的输入流,键盘
给定一个目标字符串s,一个单词集合words.要求使得words集合中所有...
class Solution(object):def findSubstring(self, s, words):""":type s: str:type words: List[str]:rtype: List[int]"""result = [];wordDict = {word:0 for word in words};matchNum = {word:0 for word in words};wordIndex = {word:[] for word in words};for word in words:matchNum[word] += 1; #记录每个单词在单词组出现次数sLen = len(s);wordLen = len(words[0]);wordNum = len(words);for i in range(wordLen):left = i; #左窗口right = i + wordLen; #右窗口while right <= sLen:substr = s[right - wordLen : right]; #在字符串中切出一个跟单词一样长度的子串if substr not in matchNum: #子串不存在于单词组里left = right; #左窗口可以直接移到这个子串后面wordDict = {word:0 for word in words}; #单词出现次数清零wordIndex = {word:[] for word in words}; #单词索引清空elif wordDict[substr] >= matchNum[substr]: #子串出现次数超过在单词组中出现次数temp = left; #记录旧的左窗口位置left = wordIndex[substr].pop(0) + wordLen; #新的左窗口移动到这个子串在窗口中第一次出现的位置的右边wordIndex[substr].append(right - wordLen); #将新的子串位置加入for t in range(temp,left - wordLen,wordLen): #将旧的左窗口到新的左窗口之间所有子串出现次数均减一wordDict[s[t:t+wordLen]] -= 1;else: #正常情况 wordDict[substr] += 1; #出现次数加一wordIndex[substr].append(right - wordLen); #将其位置加入if right - left == wordLen * wordNum: #匹配成功result.append(left);left += wordLen; #左窗口右移right = left; wordDict = {word:0 for word in words}; #清零wordIndex = {word:[] for word in words}; #清空right += wordLen; #右窗口右移wordDict = {word:0 for word in words};wordIndex = {word:[] for word in words};return result;
C#编程 定义一个函数Get Word(string,int),返回参数一string中所指定...
using System;using System.Linq;namespace Test{public class A{// 定义分隔符(分割单词,以空格为例)private static char splitChar = ' ';public static string GetWord(string str, int index){string[] words = str.Split(splitChar); // 获得分割后的各个单词if ((index < 0) || (index >= words.Count())){return ""; // 如果下标超出范围,返回空字符串}else{return words[index];}}// 姑且理解为参数1,2为取第1到第2个单词// 如果你的本意是取第1个单词开始的2个单词,那么你自己改改public static string GetWord(String str, int begin, int end){string[] words = str.Split(splitChar); // 获得分割后的各个单词if ((begin < 0) || (end >= words.Count()) || (begin > end)){return ""; // 如果下标超出范围,返回空字符串}else{string result = words[begin++];for (; begin <= end; begin++){result += splitChar + words[begin];}return result;}}public static void Main(string[] args){Console.WriteLine(GetWord("This is a dog.", 2));Console.WriteLine(GetWord("This is a dog.", 1, 2));}}}
用C++编写一个程序,从键盘接受一个字符串,按照字符从小到大的...
字符大小顺序什么意思? 我只知道单词长度(单词的字符个数)可以大小排序 同时删除重复单词// Visual Studio 2010 编译通过#include #include #include //泛型算法头文件#include using namespace std;//谓词函数bool isShorter(const string &word1, const string &word2){return word1.size() }int main(){vectorwords;string word;while ( cin >>word )words.push_back(word);sort (words.begin(), words.end()); //字典排序//排序,将重复的单词移至尾部//unique()返回尾部第一个重复单词的地址vector::iterator end_unique =unique(words.begin(), words.end()); //删除string尾部重复单词words.erase(end_unique, words.end());//按单词长度排序。
谓词isShorter()返回true或falsestable_sort(words.begin(), words.end(), isShorter);//输出排序后向量容器中的每个元素vector::const_iterator vit = words.begin();while ( vit != words.end() ) {cout ++vit;}cout system("pause");return 0;}
java中怎样把输入的基本英文单词按首字母顺序排列?
Create a PriorityQueue from a word->tf map. * * @param words a map of words keyed on the word(String) with Int objects as the values. */ private PriorityQueue createQueue(Map words) throws IOException { // have collected all words in doc and their freqs int numDocs = ir.numDocs(); final int limit = Math.min(maxQueryTerms, words.size()); FreqQ queue = new FreqQ(limit); // will order words by score for (String word : words.keySet()) { // for every word int tf = words.get(word).x; // term freq in the source doc if (minTermFreq > 0 && tf < minTermFreq) { continue; // filter out words that don't occur enough times in the source } // go through all the fields and find the largest document frequency String topField = fieldNames[0]; int docFreq = 0; for (String fieldName : fieldNames) {
编程问题:统计单词个数
#include #include // #include // 文件操作时用using namespace std;int main(void){set words;string word;while (cin >> word) // 用组合键ctrl+z或者到文件末尾结束输入words.insert(word);cout << words.size() << endl;return (0);}
用C语言做单词逆转
#include #include void reverse_words(char * words) { char * h, * t; /* head and tail pointer to a word */ if ( *words != '\n' ) { /* '\n' signals the end of the string */ for(h = words; *words != ' ' && *words != '\n'; words++) ; /* traverse the string word by word */ for(t = words-1; t >= h; t-- ) /* print a word backwards */ putchar( *t ); putchar(' '); /* print a blank space */ if ( *words != '\n' ) /* if string doesn`t end, go on processing the rest part of the string */ reverse_words(words+1); } } int main(void) { char * words; words = (char *) malloc(1024*sizeof(*words)); /* allocate a space to store the input string */ fgets(words, 1024, stdin); /* get the string */ reverse_words(words); /* do your job */ putchar('\n'); return 0; } 楼主,我这程序是用递归写的,希望你也要学会运用递归的思想思考问题。
字符串的结构本身就是递归的(你拿了一个字符,一部分字符串之后,剩下的部分还是字符串),所以我们在处理字符串的时候要习惯递归思想。
递归是非常有用的一种思考方式,你应该慢慢习惯学习并且运用它来解决诸如此类的问题。
你如果看不懂的话,尽管发消息问我。
java基本程序问题,寻找一个输入语段中出现最多次的单词,然后输出...
你好,看你的要求估计你们没有学集合,所以就给你写了个数组的:public class Words {static public int mostFrequent(int[] list) {int max = 0 ;int index = 0 ;for(int i=0;i<list.length;i++){if(max < list[i]){max = list[i] ;index = i ;}}return index ;}static public int onList(String word, String[] list) {for(int i=0;i<list.length;i++){if(word.equals(list[i])){return i ;} }return -1 ;}public static void main(String[] args) {String str = "my sister ha ha,She me love to get set Me my girl tree me" ; //测试的,这个你可以随意String[] words = str.toLowerCase().replace('.',' ').replace(',',' ').replace(" ", " ").split(" ") ;int len = words.length ;//考虑到二维数组不方便所以这里使用两个数组,二者之间是一一对应的int[] count = new int[len] ;String[] ss = new String[len] ;for (int i=0;i<words.length;i++) {String word = words[i].trim();int f = onList(word,words) ;if(f == -1){ss[i] = word ;count[i] = 1 ; }else{ss[f] = word ;count[f] += 1;}}int index = mostFrequent(count) ;System.out.println("出现次数最多的单词是:" + ss[index] + "出现的次数为:" + count[index]);}}
转载请注明出处51数据库 » string word words