被忽略的java.io.StreamTokenizer

Java StreamTokenizer详解
本文介绍Java中的StreamTokenizer类,详细讲解其使用方法及常见配置,如设置注释字符、普通字符与单词字符等,并通过实例演示如何进行字符串分割。
一直以来,偶们都知道字符串的分割最常用的是java.util.StringTokenizer,但是某些时候,StringTokenizer有很大的局限性,孰不知,有个更为强大的的分割工具,不过是基于流的,但是借助于中间类,也可以处理字符串,而且处理的很好,今天偶们就掀开他地盖头来瞧一瞧:
   StreamTokenizer定义了几种基本的常量用于标识解析过程:TT_EOF(流结尾)、TT_EOL(行结尾)、TT_NUMBER(数字符号, 0 1 2 3 4 5 6 7 8 9 . -都属于数字语法)、TT_WORD(一个单词)。
   其含有的基本方法介绍一下:
   commenChar(int ch) - 指定某个字符为注释字符,此字符之后直到行结尾都被stream tokenizer忽略。
   eolIsSignificant(boolean flag) - 决定一个行结束符是否被当作一个基本的符号处理,如果是true,则被当作一个基本符号,不当作普通的分隔符,如果是false,则保持原义,即当作普通的分隔符。
   lineno() - 返回当前流所在的行号。
   lowerCaseMode(boolean flag) - 决定是否读取一个单词时是否转变成小写。
   nextToken() - 不用解释了吧。。。
   ordinaryChar(int ch) - 指定字符在这个tokenizer中保持原义,即只会把当前字符认为普通的字符,不会有其他的语义。
   ordinaryChars(int low, int hi) - 指定范围内的字符保持语义,同上
   parseNumbers() - 当stream tokenizer遭遇到一个单词为双精度的浮点数时,会把它当作一个数字,而不是一个单词。
   pushBack() - 回退,会引起下一个nextToken方法返回当前值。
   quoteChar(int ch) - 指定当前字符为当前tokenizer中的分隔符,在两个符号之间被当作一个字符串解析。
   resetSyntax() - 重置语法表使所有的字符都被认为是“ordinary”。
   slashSlashComments(boolean flag) - 如果为true,则/*与*/之间的都被认为是注释,反之,不是。
   slashStartComments(boolean flag) - 如果为true,则//之后到行结尾的所有都被认为是注释,反之,不是。 
   whitespaceChars(int low, int hi) - 字符low与hi之间的所有字符都被当作为空格符,即被认识为tokenzier的分隔符。
   wordChars(int low, int hi) - 字符low与hi之间的所有字符都被当作为单词的要素。一个单词是由一个单词要素后面跟着0个或者更多个单词要素或者数字要素。
   api不是很复杂吧~应该不难理解。
   一下举个例子会一幕了然: // Set up a StreamTokenizer on the characters in this String
   StreamTokenizer  st  = new  StreamTokenizer (new  StringReader (svalue ));
  //知道该怎么解析字符串了吧。
   st.whitespaceChars (',',',');
   // Commas are delimiters
   st.ordinaryChars ('0''9');
   // Needed to turn off numeric flag
   st.ordinaryChars ('.''.');
   st.ordinaryChars ('-''-');
   st.wordChars ('0''9');
   // Needed to make part of tokens
   st.wordChars ('.''.');
   st.wordChars ('-''-');
   // Split comma-delimited tokens into a List
   ArrayList  list  = new  ArrayList ();
   while  (true ) 
  {
       int  ttype  = st.nextToken ();
       if  ((ttype  == StreamTokenizer.TT_WORD ) || (ttype  > 0 )) 
      {
           list.add (st.sval );
           
  
    } else  if  (ttype  == StreamTokenizer.TT_EOF ) 
      {
           break ;
           
  
    } else  
      {
           throw  new  ConversionException  ("Encountered token of type " + ttype );
           
  
    } 
  
}
import java.io.*; public class Main { static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static String nextString() throws IOException { in.nextToken(); return in.sval; } public static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { int n = nextInt(); // 读入n行数据 char[] str; // 每行的字符串 int str_num[] = new int[10]; // 字符串长度不超过10 // 按字符串顺序存储每个字符的序列, a存1, b存2 while (n-- > 0) { int sum = 0; //初始化为0 str = nextString().toCharArray(); // 输入的String转成char boolean flag = false; // 非法字符串标志为false // 从字符串第一个字符开始,挨个与后面相邻的作比较,判断是否为非法字符串 for (int i = 0; i < str.length - 1; i++) if (str[i] >= str[i + 1] || str[i] < 'a' || str[i] > 'z') { flag = true; //如果不是升序字符串或者有非小写字母的, 非法字符串标记true break; } // 非法字符串输出0 if (flag) { System.out.println(sum); continue; } // 单个字母a为1, b为2, 后面加的都是除了本身的 // 比如a b c d, d为4, sum = 3(前三个), 还得补上本身的 // 比如ab是27, sum + 前面26个字母 = 26, 所以得先加一个1 sum++; // 统计每个字母的编码, a = 1, b = 2 ...... z = 26 for (int i = 0; i < str.length; i++) str_num[i] = str[i] - 'a' + 1; // 计算小于当前长度的所有字符串情况 // 比如字符串dfgh, 计算字符串长度为1, 2, 3的所有情况 // a-z ab - yz abc - xyz abcd - dfgh // 组合公式 for (int i = 1; i < str.length; i++) sum += C(26, i); // 从初始字母a开始, temp即为1 // 统计abcd - dfgh中间的个数 // abcd - axyz bcde - bxyz cdef - cxyz defg - dfgh // d efg - d exy d fgh // dfgh在str_num中存为{4, 6, 7, 8} int temp = 1; for (int i = str.length; i > 0; i--) { // 字符串越来越短 //从temp 计算到 str_num, a算到d(不包含d) for (int j = temp; j < str_num[str.length - i]; j++) sum += C(26 - j, i - 1); //dfgh第一次循环计算abcd - axyz bcde - bxyz cdef - cxyz temp = str_num[str.length - i] + 1; //temp存当前str_num元素 + 1 // dfgh第一次循环temp = 'd' - 'a' + 1 + 1 = 5; 也就是‘e’ // d efg - d exy d fgh } out.println(sum); out.flush(); } out.close(); } //计算C(n,m) = n! / (m! * (n-m)!) public static int C(int n, int m) { int a, b; a = 1; b = 1; for (int i = n; i > n - m; i--) a *= i; for (int i = 1; i <= m; i++) b *= i; return a / b; } } ———————————————— 版权声明:本文为优快云博主「yanhua_tj」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_42898536/article/details/108555714
最新发布
09-28
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, sc, out); out.close(); } static class Task { public boolean aStar(int begin,int end,ArrayList<int[]>[] G,long[] f,int k,int n,int lim) { PriorityQueue<long[]> que=new PriorityQueue<long[]>(new Comparator<long[]>() { @Override public int compare(long[] o1, long[] o2) { if(o1[1]>o2[1]) return 1; if(o1[1]<o2[1]) return -1; return 0; } }); if(f[begin]==Integer.MAX_VALUE) return false; int[] cnt=new int[n+1]; que.offer(new long[] {begin,f[begin],0}); while(!que.isEmpty()) { long[] temp=que.poll(); int u=(int) temp[0]; cnt[u]+=1; if(cnt[u]>k) continue; if(cnt[u]==k&&u==end) { return temp[1]<=lim; } for(int[] edge:G[u]) { if(cnt[edge[0]]<k) { que.offer(new long[] {edge[0],temp[2]+f[edge[0]]+edge[1],temp[2]+edge[1]}); } } } return false; } public void solve(int testNumber, InputReader sc, PrintWriter out) throws IOException { while(sc.hasNext()) { int n=sc.nextInt(); int m=sc.nextInt(); int s=sc.nextInt(); int t=sc.nextInt(); int k=sc.nextInt(); int lim=sc.nextInt(); ArrayList<int[]>[] G=new ArrayList[n+1]; ArrayList<int[]>[] backG=new ArrayList[n+1]; for(int i=0;i<=n;i++) { G[i]=new ArrayList<int[]>(); backG[i]=new ArrayList<int[]>(); } for(int i=0;i<m;i++) { int u=sc.nextInt(); int v=sc.nextInt(); int w=sc.nextInt(); G[u].add(new int[] {v,w}); backG[v].add(new int[] {u,w}); } PriorityQueue<long[]> que=new PriorityQueue<long[]>(new Comparator<long[]>() { @Override public int compare(long[] o1, long[] o2) { if(o1[1]>o2[1]) return 1; if(o1[1]<o2[1]) return -1; return 0; } }); que.offer(new long[] {t,0}); long[] f=new long[n+1]; Arrays.fill(f, Integer.MAX_VALUE); f[t]=0; boolean[] vis=new boolean[n+1]; while(!que.isEmpty()) { long[] temp=que.poll(); int u=(int) temp[0]; long cost=temp[1]; if(vis[u]) continue; vis[u]=true; for(int[] edge:backG[u]) { if(f[edge[0]]>cost+edge[1]) { f[edge[0]]=cost+edge[1]; que.offer(new long[] {edge[0],f[edge[0]]}); } } } out.println(aStar(s,t,G,f,k,n,lim)?"yareyaredawa":"Whitesnake!"); } } } static class InputReader{ StreamTokenizer tokenizer; public InputReader(InputStream stream){ tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream))); tokenizer.ordinaryChars(33,126); tokenizer.wordChars(33,126); } public String next() throws IOException { tokenizer.nextToken(); return tokenizer.sval; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean hasNext() throws IOException { int res=tokenizer.nextToken(); tokenizer.pushBack(); return res!=tokenizer.TT_EOF; } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } public BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } } }改写成c++代码
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值